﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gencode.PrsCommon.Data.Dienst;

namespace Personalplaner.Gui.Mask
{
	[ToolboxItem(false)]
	public partial class DienstZeitDlg : UserControl, Gencode.Gengui.Forms.IDialogControl
	{
		public enum Timetype { Pause, Dienstzeit };

		DataView _zeiten;
		int _dienstID;
		Timetype _timeType;

		public DienstZeitDlg(int dienstID)
		{
			InitializeComponent();
			_dienstID = dienstID;
			InitializeGrid();

			comboType.Items.Add(Timetype.Pause.ToString());
			comboType.Items.Add(Timetype.Dienstzeit.ToString());

			InitializeDialogControls();
		}

		private void InitializeGrid()
		{
			_zeiten = DienstData.GetDienstZeiten(_dienstID);

			zeitenGrid.AutoGenerateColumns = false;
			zeitenGrid.DataSource = _zeiten;

			zeitenGrid.Columns["typ"].DataPropertyName = "typ";
			zeitenGrid.Columns["von"].DataPropertyName = "von";
			zeitenGrid.Columns["bis"].DataPropertyName = "bis";
			zeitenGrid.Columns["abteilungszeit"].DataPropertyName = "abteilungszeit";
			zeitenGrid.Columns["datvon"].DataPropertyName = "datumvon";
			zeitenGrid.Columns["datbis"].DataPropertyName = "datumbis";
		}

		private void InitializeDialogControls()
		{
			gultVon.Value = DateTime.Now;
			gultBis.Value = new DateTime(DateTime.Now.AddYears(20).Year, DateTime.Now.Month, DateTime.Now.Day);

			timeVon.Text = "07:00";
			timeBis.Text = "18:00";

			checkGanzerTag.Checked = false;
			comboType.SelectedIndex = 1;
		}

		#region IDialogControl Members

		public bool OKClicked()
		{
			//just close...
			return true;
		}

		public bool CancleClicked()
		{
			//just close...
			return true;
		}

		#endregion

		private void checkGanzerTag_CheckedChanged(object sender, EventArgs e)
		{
			if (checkGanzerTag.Checked)
			{
				timeVon.Enabled = false;
				timeBis.Enabled = false;
				label3.Enabled = false;
				label4.Enabled = false;
			}
			else
			{
				timeVon.Enabled = true;
				timeBis.Enabled = true;
				label3.Enabled = true;
				label4.Enabled = true;
			}
		}

		private void gultVon_ValueChanged(object sender, EventArgs e)
		{
			if (gultVon.Value.Day != 1)
			{
				gultVon.Value = new DateTime(gultVon.Value.Year, gultVon.Value.Month, 1);
			}
		}

		private void gultBis_ValueChanged(object sender, EventArgs e)
		{
			if (gultBis.Value.Day != DateTime.DaysInMonth(gultBis.Value.Year, gultBis.Value.Month))
			{
				gultBis.Value = new DateTime(gultBis.Value.Year, gultBis.Value.Month, DateTime.DaysInMonth(gultBis.Value.Year, gultBis.Value.Month));
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			int hvon = int.Parse(timeVon.Text.Substring(0, 2));
			int mvon = int.Parse(timeVon.Text.Substring(3, 2));
			int hbis = int.Parse(timeBis.Text.Substring(0, 2));
			int mbis = int.Parse(timeBis.Text.Substring(3, 2));
			if (hvon > hbis ||
				(hvon == hbis && mvon >= mbis))
			//if (Convert.ToInt32(formatDate(timeVon.Value.ToString())) > Convert.ToInt32(formatDate(timeBis.Value.ToString())))
			{
				MessageBox.Show("Zeit-von ist grösser als Zeit-bis.\nBitte setzen Sie die Zeit-bis grösser als die Zeit-von.", "", MessageBoxButtons.OK);
				return;
			}
			else
			{
				SetZeit stZt = new SetZeit();

				if (_timeType == Timetype.Pause)
				{
					SetZeit.SetDienstZeit(_dienstID, timeVon.Text, timeBis.Text, "P", gultVon.Text, gultBis.Text, checkGanzerTag.Checked); // P = Pause
				}
				else if (_timeType == Timetype.Dienstzeit)
				{
					SetZeit.SetDienstZeit(_dienstID, timeVon.Text, timeBis.Text, "Z", gultVon.Text, gultBis.Text, checkGanzerTag.Checked); // Z = Dienstzeit
				}
			}


			InitializeDialogControls();
			InitializeGrid();
		}

		private void comboType_SelectedIndexChanged(object sender, EventArgs e)
		{
			_timeType = (Timetype)comboType.SelectedIndex;
		}

		private void DienstZeitDlg_Load(object sender, EventArgs e)
		{
			gultVon.Value = DateTime.Now;
			gultBis.Value = new DateTime(DateTime.Now.AddYears(20).Year, DateTime.Now.Month, DateTime.Now.Day);
		} 
	}
}
